package com.raysweather.android;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class POTDXMLHandler extends DefaultHandler {

	Boolean currentElement = false;
	String currentValue = "";
	public static POTDList potdList = null;

	public static POTDList getSitesList() {
		return potdList;
	}

	public static void setSitesList(POTDList sitesList) {
		POTDXMLHandler.potdList = sitesList;
	}

	/** Called when tag starts ( ex:- <name>AndroidPeople</name>
	 * -- <name> )*/
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		currentElement = true;

		if (localName.equals("photo_of_the_day"))
		{
			/** Start */
			potdList = new POTDList();
		} else if(localName.equals("date"))
			currentValue = "";
		else if(localName.equals("url"))
			currentValue = "";
		

	}

	/** Called when tag closing ( ex:- <name>AndroidPeople</name>
	 * -- </name> )*/
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		currentElement = false;

		/** set value */
		if (localName.equalsIgnoreCase("text"))
			potdList.setText(currentValue);
		else if (localName.equalsIgnoreCase("date"))
			potdList.setDate(currentValue);
		else if (localName.equalsIgnoreCase("url"))
			potdList.setUrl(currentValue);
		else
			potdList.setText(currentValue);
	}

	/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
	 * -- to get AndroidPeople Character ) */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		if (currentElement) {
			currentValue += new String(ch, start, length);
			currentElement = false;
		}

	}

}
